﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;

namespace Bonobo.Reader.UI
{
    public class BonoboDataTemplateSelector : DataTemplateSelector
    {
        private Dictionary<Type, DataTemplate> _templateDictionary;

        public BonoboDataTemplateSelector() : base()
        {
            _templateDictionary = new Dictionary<Type, DataTemplate>();
            var loader = BonoboItemProviderLoader.GetInstance();
            var providers = loader.Providers;

            foreach (var provider in providers)
            {
                if (!_templateDictionary.ContainsKey(provider.ItemType))
                {
                    _templateDictionary.Add(provider.ItemType, provider.ItemDataTemplate);
                }
            }
        }

        public override System.Windows.DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
        {       
            var dataTemplate = new DataTemplate();
            _templateDictionary.TryGetValue(item.GetType(), out dataTemplate);
            return dataTemplate;
        }
    }
}
